package com.ly.hotel.view.fragments.game;

import android.app.AlertDialog;
import android.content.Context;
import android.content.DialogInterface;
import android.content.DialogInterface.OnClickListener;
import android.content.Intent;
import android.os.Bundle;
import android.util.Log;
import android.view.KeyEvent;
import android.view.View;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.GridView;
import android.widget.TextView;

import com.ly.hotel.R;
import com.ly.hotel.adaptor.HardwareInterface;
import com.ly.hotel.common.MyApplication;
import com.ly.hotel.common.MyLogsManager;
import com.ly.hotel.model.AppInfo;
import com.ly.hotel.msgevent.GetGameEvent;
import com.ly.hotel.msgevent.SendLogEvent;
import com.ly.hotel.msgevent.UpdateGameListEvent;
import com.ly.hotel.util.AppUtil;
import com.ly.hotel.util.ProcessWaitingUtil;
import com.ly.hotel.util.ToastUtil;
import com.ly.hotel.util.WebInstaller;
import com.ly.hotel.view.common.BaseActivity;
import java.util.ArrayList;
import java.util.List;

import de.greenrobot.event.EventBus;

public class GameActivity extends BaseActivity {
	private GridView mGridView;
	private String TAG = "GameActivity";
	private MyApplication app;
	private String titleName;

	private List<AppInfo> appInfos  = new ArrayList<>();

	
	public static void actionStart(Context context,String title){
		Intent intent = new Intent(context,GameActivity.class);
		intent.putExtra("title", title);
		context.startActivity(intent);
	}
	
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_game);
		app = (MyApplication) getApplicationContext();
		Intent intent = getIntent();
		titleName = intent.getStringExtra("title");		
		TextView title = findViewById(R.id.act_title).findViewById(R.id.activity_name);
		title.setText(titleName);
		mGridView = findViewById(R.id.mGameGridView);
	}	

	@Override
	public void onDestroy() {
		super.onDestroy();
	}
		
	@Override
	public void finish() {
		super.finish();
	}

	@Override
	public void onResume() {
		super.onResume();
	}

	@Override
	public void onPause() {
		super.onPause();
	}

	@Override
	public void onStart() {
		super.onStart();
		setValue();
		setListener();
	}

	private void setValue() {
		List<AppInfo> infos = null;
		if(titleName.equals(getResources().getString(R.string.naviGameName))){
			infos = app.getAppInfo();
		}else{
			infos = app.getThirdApps();
		}

		if (infos != null && !infos.isEmpty()) {
			appInfos.clear();
			appInfos.addAll(infos);
			AppAdapter mAdapter = new AppAdapter(this, appInfos);
			mGridView.setAdapter(mAdapter);
		}else{
			//没有数据就请求获得之
			EventBus.getDefault().post(new GetGameEvent());
			ProcessWaitingUtil.showProcessDialog(this, "Loading......", 3000);
		}
	}

	private void setListener() {
		mGridView.setOnItemClickListener(new OnItemClickListener() {
			@Override
			public void onItemClick(AdapterView<?> parent, View view, int position, long id) {

				final AppInfo apps = appInfos.get(position);
				if (!AppUtil.checkAppIsExist(GameActivity.this, apps.getPackageName())) {

					// 提示是否下载安装
					new AlertDialog.Builder(GameActivity.this).setTitle("提示").setMessage("该应用尚未安装,是否要下载安装?")
							.setPositiveButton("是", new OnClickListener() {

								@Override
								public void onClick(DialogInterface dialog, int which) {
									dialog.cancel();
									String path = apps.getPath().replace(" ", "%20");
									new WebInstaller(GameActivity.this, MyApplication.getHostResUrl() + "upload/"
											+ path).downloadAndInstall();
									HardwareInterface.setMuteFlag(false);
								}
							}).setNegativeButton("否", new OnClickListener() {

								@Override
								public void onClick(DialogInterface dialog, int which) {
									dialog.cancel();
								}
							}).show();

				} else {// 启动apk
					EventBus.getDefault().post(new SendLogEvent(MyLogsManager.CLOUD_LOGS_GAME_PREFIX + apps.getAppName()));
					AppUtil.startApp(GameActivity.this, apps.getPackageName());
					HardwareInterface.setMuteFlag(false);
				}
			}
		});
		
		mGridView.setOnKeyListener(new View.OnKeyListener() {
			
			@Override
			public boolean onKey(View arg0, int arg1, KeyEvent arg2) {
				// TODO Auto-generated method stub
				Log.i("keycode", arg1 + "key:" + arg2.getKeyCode());
				return false;
			}
		});
	}
	
	public void onEventMainThread(UpdateGameListEvent e) {
		ProcessWaitingUtil.closeProcessDialog();
		Log.i(TAG, "更新数据:" + e.getAppType() + ":" + e.getValidUpdate());
		if(titleName.equals(getResources().getString(R.string.naviGameName)) && e.getAppType().equals("game")){
			if (e.getValidUpdate()) {
				setValue();
			} else {
				ToastUtil.showShortToast(this, "获取游戏数据失败,请返回再试", 3000);
			}
		}else{
			if (e.getValidUpdate()) {
				setValue();
			} else {
				ToastUtil.showShortToast(this, "获取应用数据失败,请返回稍后再试", 3000);
			}
		}
	}
}
